<?php

namespace Home\Controller;


class ArticleController extends MainController {

    /**
     * 分类文章列表
     */
	public function cateList()
    {
		$cate = $this->getInt('cate');
		$sub = $this->getInt('sub');
		$keyword = $this->getString('keyword');
        if($cate > 0 || $keyword != '')
        {
            $aModel = M('Article');
            $cModel = M('Category');

            //子分类
            $subCateList = $cModel->where("`parent_id`={$cate}")->order("`node_order` DESC,`node_id` ASC")->select();

            //分类下的文章
            $articleList = $aModel->where("`category_id`={$cate}")->order("`article_id` ASC")->select();

            if($subCateList == false && $articleList == false)
            {
                $this->redirect('Index/index');exit;
            }
            else
            {
                if(count($articleList) == 1 && $subCateList == false)
                {
                    echo '<script>window.location="/article-'.$articleList[0]['article_id'].'.html";</script>';exit;
                }

                if($this->visitor == 'mo'){
                    if($subCateList)
                    {
                        foreach($subCateList AS $key=>$val)
                        {
                            //子分类的下级分类
                            $subList = $cModel->where("`parent_id`={$val['node_id']}")->order("`node_order` DESC,`node_id` ASC")->select();
                            $subCateList[$key]['subcate'] = $subList ? $subList : 0;

                            //子分类的文章
                            $subArticleList = $aModel->where("`category_id`={$val['node_id']}")->order("`article_id` ASC")->select();
                            $subCateList[$key]['article'] = $subArticleList ? $subArticleList : 0;
                        }
                    }

                    $this->assign('subCateList',$subCateList);

                }else{
					
					if($keyword != ''){
						$map['title'] = array('LIKE',"%".$keyword."%");
					}else{
						$cateArray = array($cate);
						if($subCateList){
							foreach($subCateList AS $key=>$val){
								array_push($cateArray,$val['node_id']);
							}
						}
						$map['category_id'] = array('IN',$cateArray);
					}
                    $articleList = $aModel->where($map)->order("`article_id` ASC")->select();
					                   
                }


                $cateinfo = $cModel->find($cate);
                $this->assign('cateinfo',$cateinfo);
				$this->assign('articleList',$articleList);
				
				$this->assign('cate',$cate);
				$this->assign('sub',$sub);
				
				$this->assign('subinfo',$sub > 0 ? $cModel->find($sub) : "");
                $this->assign('pageTitle',$cateinfo['node_name'].' - 聊城地方馆资源平台');

                $this->theme($this->visitor)->display();
            }


        }
        else
        {
            $this->redirect('Index/index');
        }

	}
	
	
	public function details()
    {
		$aModel = M('Article');
		$id	= $this->getInt('id');

        if($id > 0)
        {
            $info = $aModel->find($id);
            if($info)
            {
                $cModel = M('Category');
                $this->assign('info',$info);
                $this->assign('catename',$cModel->where("`node_id`={$info['category_id']}")->getField('node_name'));
                //更新访问量
                $aModel->where('article_id='.$id)->setInc('hits');

                $this->assign('pageTitle',$info['title'].' - 聊城地方馆资源平台');

                $this->theme($this->visitor)->display();
            }
            else
            {
                $this->redirect('Index/index');
            }
        }
        else
        {
            $this->redirect('Index/index');
        }

	}
	
}